package com.ccl.iot.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ccl.iot.R;

/**
 * 创建者     王浩
 * 创建时间   2016/12/28 14:47
 * 描述	      ${TODO}
 */
public class WaitRegView extends View {

    private RectF mRect;
    private Paint mPaint;
    private float mAngle = 270;
    private boolean init = false;
    private int mWidth;
    private int mHeight;
    private boolean mClickable;
    private int mWaitTime;
    private float mStepAngle;

    public WaitRegView(Context context) {
        this(context, null);
    }

    public WaitRegView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaitRegView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaitView);
        mClickable = typedArray.getBoolean(R.styleable.WaitView_onWaitClickEnable, false);
        mWaitTime = typedArray.getInt(R.styleable.WaitView_waitTime, 3000);
        mStepAngle = (float) (360.0 / (mWaitTime / 100));
        init();
    }

    private void init() {
        setBackgroundColor(Color.TRANSPARENT);
        mRect = new RectF(0, 0, 0, 0);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.argb(55, 40, 40, 40));
        setVisibility(View.GONE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getWidth();
        mHeight = getHeight();
        mRect.left = 0 - mWidth;
        mRect.top = 0 - mHeight;
        mRect.right = mWidth << 1;
        mRect.bottom = mHeight << 1;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (init && mAngle >= 270) {
            this.setVisibility(View.GONE);
            init = false;
            if (mWaitDoneListener != null) {
                mWaitDoneListener.onWaitDone();
            }
            return;
        }
        if (mAngle > 360) {
            init = true;
            mAngle = 0;
        }

        float angle;
        if (mAngle >= 270) {
            angle = 360 - (mAngle - 270);
        } else {
            angle = 270 - mAngle;
        }

        canvas.drawArc(mRect, mAngle, angle, true, mPaint);
        mAngle += mStepAngle;

        //        LoggerUtil.i("123","矩阵：" +mRect + "--高度：" +getHeight() + "--宽度：" +getWidth()+ "--角度：" + mAngle + "--角度2："+ angle  );
        postDelayed(mTask, 80);
    }

    private Runnable mTask = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.GONE) {
            mAngle = 270;
            init = false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mClickable) {

            return super.onTouchEvent(event);
        }
        return true;
    }

    private OnWaitDoneListener mWaitDoneListener;

    public interface OnWaitDoneListener {
        public void onWaitDone();
    }

    public void setOnWaitDoneListener(OnWaitDoneListener listener) {
        mWaitDoneListener = listener;
    }
}
